#include <iostream>
#include <list>
using namespace std;

class Student {
public:
    Student(string name, int chinese, int math, int english)
    : _name(name)
    , _chinese(chinese)
    , _math(math)
    , _english(english)
    , _sum(chinese + math + english)
    { }

    string _name;
    int _chinese;
    int _math;
    int _english;
    int _sum;
};

// 1、运算符重载
bool operator>(const Student & lhs, const Student &rhs){
    if (lhs._sum > rhs._sum){
        return true;
    } else if (lhs._sum  == rhs._sum){
        if (lhs._chinese > rhs._chinese){
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

// 2、函数对象
struct CompareStudent{
    bool operator()(const Student &lhs, const Student &rhs){
        if (lhs._sum > rhs._sum){
            return true;
        } else if (lhs._sum  == rhs._sum){
            if (lhs._chinese > rhs._chinese){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
};

// 3、函数指针
bool CompareStudent(const Student &lhs, const Student &rhs){
    if (lhs._sum > rhs._sum){
        return true;
    } else if (lhs._sum  == rhs._sum){
        if (lhs._chinese > rhs._chinese){
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}


void test(){
    list<Student> lst;
    lst.push_back(Student("zhangSan", 98, 95, 78));
    lst.push_back(Student("liSi", 58, 41, 12));
    lst.push_back(Student("WangWu", 45, 98, 35));
    lst.push_back(Student("ZhaoLiu", 54, 56, 84));

    for (auto elem : lst){
        cout << "name = " << elem._name 
            << " \tchinese: " << elem._chinese 
            << " \tmath: " << elem._math
            << " \tenglish: " << elem._english
            << " \tsum: " << elem._sum << endl;
    }

    cout << "---------------------------------" << endl;
    // lst.sort(greater<Student>());    // 运算符重载
    // lst.sort(CompareStudent());      // 函数对象
    lst.sort(CompareStudent);           // 函数指针
    
    cout << "After: "<< endl;
    for (list<Student>::iterator it = lst.begin(); it != lst.end(); ++it)
    {
        cout << "Name: " << it->_name 
            << " Chinese: " << it->_chinese
            << " Math: " << it->_math 
            << " English: " << it->_english 
            << " Sum: " << it->_sum << endl;
    }
}

int main(){
    test();

    return 0;
}